﻿
using System;
using System.IO;
using com.ljsd.jieling.protocols;
using ProtoBuf;
using Unity.VisualScripting;
using UnityEngine;

public static class NetManager
{
    public static void LoginRequest(string openId, Action<LoginResponse> callbalck)
    {
        SocketClient client = NetworkManager.Instance.GetSocket(SocketType.LOGIN);
        if (client == null)
        {
            Debug.LogError("LoginRequest: 未找到登录类型的SocketClient");
            return;
        }
        if (!client.IsConnected())
        {
            Debug.LogError("LoginRequest: Socket客户端未连接");
            return;
        }
        Debug.Log("发送登录请求");
        // NetworkManager.Instance.GetSocket(SocketType.LOGIN).SendHeartBeat();
        //TODO:发送登录请求
        LoginRequest data = new LoginRequest();
        data.device_id_s = "";
        data.idfa_sOr_imei_s = "";
        data.brand_type_s = "";
        data.brand_s = "";
        data.os_version_s = "";
        data.dpi_s = "";
        data.operator_s = "";
        data.network_s = "";
        data.ip_s = "";
        data.distinct_id = "";
        data.openId = openId;
        data.channel_s = "";
        data.platform_s = "PC";
        Debug.LogError("AppConst.LoginRoot_Version:"+ AppConst.LoginRoot_Version);
        byte[] bytes;
        using (MemoryStream stream = new MemoryStream())
        {
            Serializer.Serialize(stream, data);
            bytes = stream.ToArray(); // 获取序列化后的字节数组
        }
        NetworkManager.Instance.GetSocket(SocketType.LOGIN).SendMessageWithCallBack((int)MessageType.LOGIN_REQUEST,(int)MessageType.LOGIN_RESPONSE, bytes,
            (buff) =>
            {
                Debug.Log("登录成功");
                using (MemoryStream stream = new MemoryStream(buff.DataByte().buffer))
                {
                    LoginResponse data = Serializer.Deserialize<LoginResponse>(stream);
                    Debug.LogError(data.newToken);
                    if (data.resultCode == 0)
                    {
                        Debug.Log("登录成功");
                    }
                    callbalck?.Invoke(data);
                    AppConst.Token = int.Parse(data.newToken);
                }
            });
    }

    public static void ReceiveErrorInfo(byte[] msg)
    {
        
        using (MemoryStream stream = new MemoryStream(msg))
        {
            ErrorResponse data = Serializer.Deserialize<ErrorResponse>(stream);
            Debug.LogError(data.errCode);
        }
    }

    // public static void GetAllMailData(Action callbalck)
    // {
    //     NetworkManager.Instance.GetSocket(SocketType.LOGIN).SendMessageWithCallBack((int)MessageType.GET_ALL_MAIL_INFO_REQUEST,(int)MessageType.GET_ALL_MAIL_INFO_RESPONSE, null,
    //         (buff) =>
    //         {
    //             Debug.Log("收到邮件列表");
    //             using (MemoryStream stream = new MemoryStream(buff.DataByte().buffer))
    //             {
    //                 GetAllMailInfoResponse data = Serializer.Deserialize<GetAllMailInfoResponse>(stream);
    //                 MailManager.Instance.InitMailDataList(data);
    //                 callbalck?.Invoke();
    //             }
    //         });
    // }
    // public static void GetAllBagData(Action callbalck)
    // {
    //     NetworkManager.Instance.GetSocket(SocketType.LOGIN).SendMessageWithCallBack((int)MessageType.GET_ITEMINFO_REQUEST, (int)MessageType.GET_ITEMINFO_RESPONSE, null,
    //         (buff) =>
    //         {
    //             Debug.Log("收到背包列表");
    //             using (MemoryStream stream = new MemoryStream(buff.DataByte().buffer))
    //             {
    //                 GetItemInfoResponse itemdata = Serializer.Deserialize<GetItemInfoResponse>(stream);
    //                 BagManager.Instance.InitBagDataList(itemdata);
    //                 callbalck?.Invoke();
    //             }
    //         });
    // }
    // public static void GetAllMarketData(Action callbalck)
    // {
    //     NetworkManager.Instance.GetSocket(SocketType.LOGIN).SendMessageWithCallBack((int)MessageType.GET_STARE_INFOS_REQUEST, (int)MessageType.GET_STARE_INFOS_RESPONSE, null,
    //         (buff) =>
    //         {
    //             Debug.Log("收到黑市列表");
    //             using (MemoryStream stream = new MemoryStream(buff.DataByte().buffer))
    //             {
    //                 GetStoreInfosResponse data = Serializer.Deserialize<GetStoreInfosResponse>(stream);
    //                 MarketManager.Instance.InitMarkerDataList(data);
    //                 callbalck?.Invoke();
    //             }
    //         });
    // }
    //
    //
    // public static void GetAllEquipData(Action callblack)
    // {
    //     NetworkManager.Instance.GetSocket(SocketType.LOGIN).SendMessageWithCallBack((int)MessageType.GET_ALL_EQUIP_REQUEST, (int)MessageType.GET_ALL_EQUIP_RESPONSE, null,
    //         (buff) =>
    //         {
    //             Debug.Log("收到研究所列表");
    //             using (MemoryStream stream = new MemoryStream(buff.DataByte().buffer))
    //             {
    //                 GetAllEquipResponse data = Serializer.Deserialize<GetAllEquipResponse>(stream);
    //                 InstituteManager.Instance.InitInstituteDataList(data);
    //                 callblack?.Invoke();
    //             }
    //         });
    // }
    //
    // public static void GetAllHero(Action callblack) {
    //     NetworkManager.Instance.GetSocket(SocketType.LOGIN).SendMessageWithCallBack((int)MessageType.GET_HEROINFO_REQUEST, (int)MessageType.GET_HEROINFO_RESPONSE, null,
    //         (buff) =>
    //         {
    //             Debug.Log("收到英雄列表");
    //             using (MemoryStream stream=new MemoryStream(buff.DataByte().buffer)) {
    //                 GetHeroListInfoResponse data = Serializer.Deserialize<GetHeroListInfoResponse>(stream);
    //                 HeroMainManager.Instance.InitHeroDataList(data);
    //                 callblack?.Invoke();
    //             }
    //         });
    // }
    //
    // //接收消息请求
    // internal static void GetAllChatData(Action callbalck)
    // {
    //     GetChatMessageInfoRequest data = new GetChatMessageInfoRequest();
    //     data.messageId = 0;
    //     data.chatType = 1;
    //     byte[] bytes;
    //     using (MemoryStream stream = new MemoryStream())
    //     {
    //         Serializer.Serialize(stream, data);
    //         bytes = stream.ToArray(); // 获取序列化后的字节数组
    //     }
    //     NetworkManager.Instance.GetSocket(SocketType.LOGIN).SendMessageWithCallBack((int)MessageType.GET_CHAT_MESSAGE_REQUEST, (int)MessageType.GET_CHAT_MESSAGE_REPONSE, bytes,
    //         (buff) =>
    //         {
    //             Debug.Log("收到消息");
    //             using (MemoryStream stream = new MemoryStream(buff.DataByte().buffer))
    //             {
    //                 GetChatMessageInfoResponse data = Serializer.Deserialize<GetChatMessageInfoResponse>(stream);
    //                 ChatManager.Instance.InitChatDataList(data);
    //                 callbalck?.Invoke();
    //             }
    //         });
    // }
    //
    // //发送消息请求
    // internal static void SendChatData(string message, Action callbalck)
    // {
    //     SendChatInfoReqest data = new SendChatInfoReqest();
    //     data.chatType = 1;
    //     data.message = message;
    //     //data.friendId = 1;
    //     byte[] bytes;
    //     using (MemoryStream stream = new MemoryStream())
    //     {
    //         Serializer.Serialize(stream, data);
    //         bytes = stream.ToArray(); // 获取序列化后的字节数组
    //     }
    //     NetworkManager.Instance.GetSocket(SocketType.LOGIN).SendMessageWithCallBack((int)MessageType.SEND_CHAT_INFO_REQUEST, (int)MessageType.SEND_CHAT_INFO_RESPONSE,bytes,
    //         (buff) =>
    //         {
    //             Debug.Log("发送消息");
    //             using (MemoryStream stream = new MemoryStream(buff.DataByte().buffer))
    //             {
    //                 SendChatInfoResponse data = Serializer.Deserialize<SendChatInfoResponse>(stream);
    //                 ChatManager.Instance.SendChatDataList(data);
    //                 callbalck?.Invoke();
    //             }
    //         });
    // }
    //
    // public static void GetAllRecruit(Action callblack)
    // {
    //     NetworkManager.Instance.GetSocket(SocketType.LOGIN).SendMessageWithCallBack((int)MessageType.DRAW_HERO_REQUEST, (int)MessageType.DRAW_HERO_RESPONSE, null,
    //         (buff) =>
    //         {
    //             Debug.Log("收到招募列表");
    //             using (MemoryStream stream = new MemoryStream(buff.DataByte().buffer))
    //             {
    //                 DrawHeroResponse data = Serializer.Deserialize<DrawHeroResponse>(stream);
    //                 RecruitManager.Instance.InitRecruitDataList(data);
    //                 callblack?.Invoke();
    //             }
    //         });
    // }
} 
